﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using fsw.Common;
using System.Text;

namespace fsw.Web.Card
{
    public partial class Card : Web.UI.ManagePage
    {
        protected Model.Admin model_admin;//登录用户名
        protected string _Path = string.Empty;
        protected string _Path_file = string.Empty;
        protected List<string> _ListCard = new List<string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            model_admin = GetAdminInfo();
            this._Path = Request.ServerVariables["APPL_PHYSICAL_PATH"];
            this._Path_file = DTKeys.Path_File;
            this._ListCard = GetListCard("");
        }
        /// <summary>
        /// 树型列表
        /// </summary>
        /// <param name="_Url"></param>
        /// <returns></returns>
        private List<string> GetListCard(string _Url)
        {
            List<string> _List = new List<string>();
            if (!DirFileHelper.IsEmptyDirectory(Server.MapPath(this._Path_file + _Url)))
            {
                string[] _dirfile = null;
                if (!string.IsNullOrEmpty(_Url))
                    _dirfile = DirFileHelper.GetDirectories(Server.MapPath(this._Path_file + _Url));
                else
                    _dirfile = GetCardList().ToArray();
                for (int i = 0; i < _dirfile.Length; i++)
                {
                    string _DirFileUrl = _dirfile[i].Replace(_Path, "/").Replace(@"\", "/");
                    string[] _DirFileUrlArr = _DirFileUrl.Split('_');
                    int _DirFilePid = Utils.StringToInt(DirFileHelper.GetFileProject(_DirFileUrlArr[0]));
                    string _DirFileProject = new BLL.Project().GetTitle(_DirFilePid);
                    string _DirFileUrl2 = _DirFileUrl.Replace(this._Path_file.Substring(1), "");
                    string _DirFileName = DirFileHelper.GetFileLastName(_dirfile[i]);
                    if (string.IsNullOrEmpty(_Url)) _DirFileName = _DirFileProject;
                    if (UserProjectLevel(_DirFilePid))
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("{");
                        sb.AppendFormat("id: '{0}', pId: '{1}', name: '{2}', url: 'List.aspx?url={3}', target: 'content_body'", _DirFileUrl2, _Url, _DirFileName, _DirFileUrl2);
                        sb.Append("}");
                        _List.Add(sb.ToString());
                        _List.AddRange(GetListCard(_DirFileUrl2));
                    }
                }
            }
            return _List;
        }
        private List<string> GetCardList()
        {
            List<string> _List = new List<string>();
            Dictionary<int, string> _ListInt = new Dictionary<int, string>();
            if (!DirFileHelper.IsEmptyDirectory(Server.MapPath(this._Path_file)))
            {
                string[] _dirfile = DirFileHelper.GetDirectories(Server.MapPath(this._Path_file));
                for (int i = 0; i < _dirfile.Length; i++)
                {
                    string _DirFileUrl = _dirfile[i].Replace(_Path, "/").Replace(@"\", "/");
                    string[] _DirFileUrlArr = _DirFileUrl.Split('_');
                    int _DirFilePid = Utils.StringToInt(DirFileHelper.GetFileProject(_DirFileUrlArr[0]));
                    int _OrderNo = GetPidOrderNo(_DirFilePid);
                    _ListInt.Add(_OrderNo, _dirfile[i]);
                }
                Dictionary<int, string> dic1desc = _ListInt.OrderByDescending(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
                foreach (KeyValuePair<int, string> k in dic1desc)
                {
                    _List.Add(k.Value);                   
                }
            }
            return _List;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        private int GetPidOrderNo(int Id)
        {
            List<Model.Project> _ListPid = new BLL.Project().GetList();
            foreach (Model.Project m in _ListPid)
            {
                if (m.Pid == Id) return m.OrderNo;
            }
            return 1000;
        }
    }
}